package com.baofu.paycore.biz.engine.processor.impl;

import com.baofu.paycore.biz.engine.processor.BaseProcessor;
import com.baofu.paycore.biz.engine.processor.ProcessorContainer;
import com.baofu.paycore.biz.enums.CommandProcessorEnum;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * 处理器容器实现类
 * <p>
 *  1、初始化处理器容器
 *  2、获取处理器
 * </p>
 */
@Service
public class ProcessorContainerImpl implements ProcessorContainer {

    /** 余额处理器 */
    @Autowired
    private BalanceProcessor                         balanceProcessor;

    /** 银行卡处理器 */
    @Autowired
    private BankProcessor                            bankProcessor;

    /** 快捷确认处理器 */
    @Autowired
    private BankConfirmProcessor                     bankConfirmProcessor;

    /** 账务充值处理器 */
    @Autowired
    private AccRechargeProcessor                     accRechargeProcessor;

    /** 余额退款处理器 */
    @Autowired
    private BalanceRefundProcessor                   balanceRefundProcessor;

    /** 银行卡退款处理器 */
    @Autowired
    private BankRefundProcessor                      bankRefundProcessor;

    /** 预授权冻结处理器 */
    @Autowired
    private PreAuthFreezeProcessor                   preAuthFreezeProcessor;

    /** 预授权解冻处理器 */
    @Autowired
    private PreAuthUnFreezeProcessor                 preAuthUnFreezeProcessor;

    /** 预授权解冻转账处理器*/
    @Autowired
    private AuthUnFreezeTransferProcessor            authUnFreezeTransferProcessor;

    /** 处理器容器 */
    private Map<CommandProcessorEnum, BaseProcessor> processors = Maps.newHashMap();

    /**
     * 初始化处理器容器
     */
    @PostConstruct
    public void init() {
        processors.put(balanceProcessor.getProcessorType(), balanceProcessor);
        processors.put(bankProcessor.getProcessorType(), bankProcessor);
        processors.put(bankConfirmProcessor.getProcessorType(), bankConfirmProcessor);
        processors.put(accRechargeProcessor.getProcessorType(), accRechargeProcessor);
        processors.put(balanceRefundProcessor.getProcessorType(), balanceRefundProcessor);
        processors.put(bankRefundProcessor.getProcessorType(), bankRefundProcessor);
        processors.put(preAuthFreezeProcessor.getProcessorType(), preAuthFreezeProcessor);
        processors.put(preAuthUnFreezeProcessor.getProcessorType(), preAuthUnFreezeProcessor);
        processors.put(authUnFreezeTransferProcessor.getProcessorType(), authUnFreezeTransferProcessor);
    }

    /**
     * 获取处理器
     *
     * @param type 支付命令处理器名称
     * @return     处理器
     */
    @Override
    public BaseProcessor getProcessor(CommandProcessorEnum type) {
        return processors.get(type);
    }

}
